package com.atguigu.gulimall.member.service.impl;

import com.atguigu.common.vo.MemberVo;
import com.atguigu.gulimall.member.entity.MemberLevelEntity;
import com.atguigu.gulimall.member.exception.PhoneExistException;
import com.atguigu.gulimall.member.exception.UsernameExistException;
import com.atguigu.gulimall.member.service.MemberLevelService;
import com.atguigu.gulimall.member.vo.LoginVo;
import com.atguigu.gulimall.member.vo.RegistVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.member.dao.MemberDao;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
    @Autowired
    private MemberLevelService memberLevelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(RegistVo registVo) {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setCreateTime(new Date());
        //设置默认的会员等级
        //查询默认会员等级
        MemberLevelEntity levelEntity  = memberLevelService.queryDefaultLevel();
        memberEntity.setLevelId(levelEntity.getId());
        //判断是否已经有改用户名或者手机号
        this.phoneExist(registVo.getPhone());
        this.usernameExist(registVo.getUsername());
        memberEntity.setMobile(registVo.getPhone());
        memberEntity.setUsername(registVo.getUsername());
        //密码加密(底层是利用md5加盐值加密)
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        memberEntity.setPassword(encoder.encode(registVo.getPassword()));

        //其他的默认信息
        this.baseMapper.insert(memberEntity);
    }

    public void phoneExist(String phone) throws PhoneExistException{
        Integer count = this.baseMapper.phoneExist(phone);
        if(count>0){
            throw new PhoneExistException();
        }
    }

    @Override
    public MemberVo login(LoginVo loginVo) {
        //根据手机号或者用户名进行查询
        String username = loginVo.getUsername();
        MemberEntity member = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", username).or().eq("mobile", username));
        if(member==null){
            return null;
        }
        //校验密码
        String password = member.getPassword();
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        boolean matches = encoder.matches(loginVo.getPassword(), password);
        if(matches){
            //登录成功
            MemberVo memberVo = new MemberVo();
            BeanUtils.copyProperties(member,memberVo);
            return memberVo;
        }
        return null;
    }

    public void usernameExist(String username) throws UsernameExistException{
        Integer count = this.baseMapper.usernameExist(username);
        if(count>0){
            throw new UsernameExistException();
        }
    }

}